from time import clock,time

class Decile_cut :
    """
    Auto cut to the decile
    """
    
    def __init__(self):
        self.data = []
        self.nb = 10
        
    def add ( self, stat ):
        self.data.append(stat)
            
    def get_res (self):
        self.results = [0]*self.nb
        if self.data != [] :
            self.data.sort ()
            for i in range(self.nb) :
                self.results[i] = (float(self.data[i*len(self.data)/self.nb]))
        return self.results

"""
class Tab_Decile :
    def __init__(self, size):
        self.data =[]
        self.size = size
        for x in range(size):
            self.data[x] = Decile_cut()
            
    Def add (self, feed ):
        if ( len(feed) != size ) :
            raise Error_size()
        for x in range(size):
"""

class Board_test :
    """
    Auto run tests
    """
    def __init__(self, board):
        self.col = [Decile_cut ()]*board.max_x
        self.row = [Decile_cut ()]*board.max_y
        
    def run( self, board ):
        for x in range(board.max_x) : 
            self.col[x].add(board.nb_in_col(x))
        for y in range(board.max_y) : 
            self.row[y].add(board.nb_in_row(y))
        
    def get_res( self):
        res = []
        for x in range(len(self.col)) : 
            res += self.col[x].get_res()
        for y in range(len(self.row)) : 
            res += self.row[y].get_res()
        return res



class Line_efficiency :
    """
    How many lines are cleared at once
    """
    def __init__(self):
        self.result = Decile_cut()

    def run(self,nb_lines):
        if (nb_lines>0) :
            self.result.add(nb_lines)

    def get_res(self):
        return self.result.get_res()

class Keyboard_sensor:
    """
    
    """
    def __init__(self):
        self.data = {"down":Decile_cut(),"rotate2":Decile_cut(),
                     "rotate1":Decile_cut(),"drop":Decile_cut(),"left":Decile_cut(),"right":Decile_cut()}
        self.time = {"down":time(),"rotate2":time(),
                     "rotate1":time(),"drop":time(),"left":time(),"right":time()}
    
    def handle (self, move):
        self.data[move].add(time()-self.time[move])
        self.time[move] = time()

    def get_res (self):
        res = []
        for i,v in self.data.iteritems() :
            res += v.get_res()
        return res
